package com.metadata_system.system.service;

import com.metadata_system.common.db.entity.Message;
import com.metadata_system.common.db.mapper.MessageMapper;
import com.metadata_system.common.db.mapper.ProjectLogMapper;
import com.metadata_system.common.db.mapper.ProjectMapper;
import com.metadata_system.common.db.mapper.ProjectUserMapper;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.service.CommonUserService;
import com.metadata_system.system.request.MessageListParams;
import com.metadata_system.system.response.MessageResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Autowired(required = false)
    MessageMapper messageMapper;
    @Autowired(required = false)
    ProjectLogMapper projectLogMapper;
    @Autowired(required = false)
    ProjectMapper projectMapper;
    @Autowired(required = false)
    ProjectUserMapper projectUserMapper;


    public ApiResult getList(MessageListParams listParams) {
        Example example = new Example(Message.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isBlank(listParams.getContent())) {
            criteria.andLike("content", "%" + listParams.getContent() + "%");
        }
        if (!StringUtils.isBlank(listParams.getType())) {
            criteria.andEqualTo("type", listParams.getType());
        }
        if (!StringUtils.isBlank(listParams.getProjectName())) {
            criteria.andLike("projectName", "%" + listParams.getProjectName() + "%");
        }
        if (listParams.getIsRead() != null) {
            criteria.andEqualTo("isRead", listParams.getIsRead());
        }
        if (listParams.getProjectId() != null) {
            criteria.andEqualTo("projectId", listParams.getProjectId());
        }
        if (listParams.getUserId() != null) {
            criteria.andEqualTo("userId", listParams.getUserId());
        }


        listParams.setOrder(example, "createdTime");
        listParams.setConditionTime(listParams.getCreatedTime(), criteria, "createdTime");


        int total = this.messageMapper.selectCountByExample(example);
        List<Message> messages = new ArrayList<>();
        if (total > 0) {
            messages = this.messageMapper.selectByExampleAndRowBounds(example, listParams.getRowRunds());
        }

        return ApiResult.successPage(MessageResponse.fromEntity(messages), total);
    }


    public void addMessage(String content, String type, Integer userId, Integer projectId, String projectName, String params) {
        this.messageMapper.insertSelective(Message.builder()
                .content(content)
                .type(type)
                .userId(userId)
                .projectId(projectId)
                .projectName(projectName)
                .createdTime(new Date())
                .isRead(0)
                .params(params)
                .build());
    }


    public ApiResult read(Integer id, Integer userId) {
        Message message = this.messageMapper.selectByPrimaryKey(id);
        if (message == null) {
            return ApiResult.error("消息不存在");
        }
        message.setIsRead(1);
        this.messageMapper.updateByPrimaryKeySelective(message);

        // 同步更新项目日志
        if (message.getType().equals("ProjectApply")) {
            this.projectLogMapper.readProjectApply(userId, Integer.valueOf(message.getParams()));
        }
        if (message.getType().equals("ProjectHandler")) {
            this.projectLogMapper.readProjectHandler(userId, Integer.valueOf(message.getParams()));
        }

        return commonUserService.getUnReadCount(userId);
    }

    @Autowired
    CommonUserService commonUserService;

    /**
     * 全部信息-标记已读
     *
     * @param userId
     * @return
     */
    public ApiResult readAll(Integer userId) {
        // 同步更新消息
        this.messageMapper.readAll(userId);

        // 同步更新项目日志
        this.projectLogMapper.readAll(userId);

        return commonUserService.getUnReadCount(userId);
    }
}
